<template>
  <div class="add-container">
    <div class="add-top">
      <el-page-header @back="goBack" :content="gobacktitle"></el-page-header>
    </div>
    <div class="add-main">
      <div class="add-main-top">
        <div class="add-main-tital">
          <p>{{ gobacktitle }}转级</p>
        </div>
        <div>
          <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-position="left" label-width="0.8rem"
            class="demo-ruleForm">
            <el-row :gutter="20">

              <el-col :span="6">
                <el-form-item label="物资分类" prop="wuzizyid">
                  <el-select v-model="ruleForm.wuzizyid" placeholder="请选择">
                    <el-option v-for="(item, index) in Wzzylist" :key="index" :label="item.zhuanyemc"
                      :value="item.zhuanyeid"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="物资区分" prop="wuziqfid">
                  <el-select v-model="ruleForm.wuziqfid" placeholder="请选择">
                    <el-option v-for="(item, index) in $util.getWzqf()" :key="index" :label="item.name"
                      :value="item.id"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="库房" prop="kufangid">
                  <el-select v-model="ruleForm.kufangid" placeholder="请选择">
                    <el-option v-for="(item, index) in KufangList" :key="index" :label="item.mingcheng"
                      :value="item.id"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="操作人">
                  <el-input v-model="ruleForm.caozuor"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="主管单位" prop="zhuguandwid">
                  <el-select v-model="ruleForm.zhuguandwid" placeholder="请选择">
                    <el-option v-for="(item, index) in Zgdwlist" :key="index" :label="item.danweimc"
                      :value="item.id"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="备注">
                  <el-input v-model="ruleForm.beizhu"></el-input>
                </el-form-item>
              </el-col>

            </el-row>
          </el-form>
        </div>
      </div>
      <div class="add-main-list">
        <div class="list-button-group">
          <div class="button-item">

            <!-- @click="insertEvent()" -->
            <el-button type="primary" icon="el-icon-plus" @click="DialogOpenWuzi([])">
              新增
            </el-button>
          </div>
          <div class="button-item">
            <el-button type="danger" icon="el-icon-delete" @click="delwzDetail">
              删除
            </el-button>
          </div>
          <!-- <div class="button-item">
            <el-button type="primary" icon="el-icon-s-check" @click="importShow = true">
              导入入库单明细
            </el-button>
          </div>
          <div class="button-item">
            <el-button type="primary" icon="el-icon-s-check">
              下载模板
            </el-button>
          </div> -->
        </div>
        <div class="table-list" id="table-list">
          <ux-grid border show-overflow ref="plxTable" keep-source :height="tabheight" :highlightCurrentRow="false"
            widthResize :rules="ruleslist" @selection-change="selectChange"
            :edit-config="{ trigger: 'click', mode: 'row' }">
            <ux-table-column type="checkbox" width="50" />
            <ux-table-column type="index" title="序号" width="50"></ux-table-column>
            <ux-table-column field="zhengshibmm" title="编目码"></ux-table-column>
            <ux-table-column field="wuzimc" title="物资名称"></ux-table-column>
            <ux-table-column field="guigexh" title="规格型号"></ux-table-column>
            <ux-table-column field="huoweimc" title="货位"></ux-table-column>
            <ux-table-column field="zhiliangdjmc" title="原质量等级"></ux-table-column>
            <ux-table-column field="biaozhundwsl" title="库存数量" :edit-render="{ autofocus: '.el-input__inner' }">
              <template v-slot:edit="scope">
                <el-input v-model.trim="scope.row.biaozhundwsl" type="number" :min="1" placeholder="请输入库存数量"
                  @keyup.native="scope.row.biaozhundwsl = oninput(scope.row.biaozhundwsl)" />
              </template>
            </ux-table-column>
            <ux-table-column field="mubiaozldjmc" title="目标质量等级"></ux-table-column>
            <ux-table-column field="zhuanjisl" title="转级数量" :edit-render="{ autofocus: '.el-input__inner' }">
              <template v-slot:edit="scope">
                <el-input v-model.trim="scope.row.zhuanjisl" type="number" :min="1" placeholder="请输入库存数量"
                  @keyup.native="scope.row.zhuanjisl = oninput(scope.row.zhuanjisl)" />
              </template>
            </ux-table-column>
            <ux-table-column field="zhuanjibzdysl" title="转级包装单元数量" :edit-render="{ autofocus: '.el-input__inner' }">
              <template v-slot:edit="scope">
                <el-input v-model.trim="scope.row.zhuanjibzdysl" type="number" :min="1" placeholder="请输入库存数量"
                  @keyup.native="scope.row.zhuanjibzdysl = oninput(scope.row.zhuanjibzdysl)" />
              </template>
            </ux-table-column>
            <ux-table-column field="jiliangdw" title="计量单位"></ux-table-column>
            <ux-table-column field="baozhuangdysl" title="包装单元数量" :edit-render="{ autofocus: '.el-input__inner' }">
              <template v-slot:edit="scope">
                <el-input v-model.trim="scope.row.baozhuangdysl" type="number" :min="1" placeholder="请输入库存数量"
                  @keyup.native="scope.row.baozhuangdysl = oninput(scope.row.baozhuangdysl)" />
              </template>
            </ux-table-column>
            <ux-table-column field="baozhuangdylxmc" title="包装单元类型" :edit-render="{ autofocus: '.el-input__inner' }">
              <template v-slot:edit="scope">
                <el-input v-model.trim="scope.row.baozhuangdylxmc" type="number" :min="1" placeholder="请输入库存数量"
                  @keyup.native="scope.row.baozhuangdylxmc = oninput(scope.row.baozhuangdylxmc)" />
              </template>
            </ux-table-column>
            <ux-table-column field="baozhuangdyzdgwzsl" title="包装单元单物资数量"
              :edit-render="{ autofocus: '.el-input__inner' }">
              <template v-slot:edit="scope">
                <el-input v-model.trim="scope.row.baozhuangdyzdgwzsl" type="number" :min="1" placeholder="请输入库存数量"
                  @keyup.native="scope.row.baozhuangdyzdgwzsl = oninput(scope.row.baozhuangdyzdgwzsl)" />
              </template>
            </ux-table-column>
          </ux-grid>
          <div class="submit-button">
            <el-button type="primary" icon="el-icon-circle-check" @click="submitForm('ruleForm')">
              确认
            </el-button>
            <el-button type="info" icon="el-icon-delete" @click="goBack">取消</el-button>
          </div>
        </div>
      </div>
    </div>

    <!--新增 物资 -->
    <page-1-add-wuzi ref="wuzi-dialog" @fetch-data="fetchData"></page-1-add-wuzi>
    <!-- 导入文件start -->
    <el-dialog :close-on-click-modal="false" :visible.sync="importShow" destroy-on-close title="导入文件" class="m-dialog"
      :append-to-body="true">
      <upload api-url="xqzdDetail/import" @success="uploadSuccess" />
    </el-dialog>
  </div>
</template>
<script>
import Upload from '@/components/Upload/upload'
import { queryKufangList } from '@/api/warehousing'
import page1AddWuzi from './page1AddWuzi.vue'
import { insertZhuanjid } from '@/api/kunei'

export default {
  name: 'Page1Add',
  components: { Upload, page1AddWuzi },
  props: {
    gobacktitle: {
      type: String,
      default: '新增',
      required: true,
    },
    datarow: {
      type: Object,
      default: function () {
        return {}
      },
    },
  },
  data() {
    return {
      show: true,
      insertData: {},
      ruleForm: {

        mxList: [],
      },
      rules: {
        wuziqfid: [{ required: true, message: '请选择物资区分', trigger: 'blur' }],
        kufangid: [{ required: true, message: '请选择库房', trigger: 'blur' }],
        wuzizyid: [{ required: true, message: '请选择物资分类', trigger: 'blur' }],
        zhuguandwid: [{ required: true, message: '请选择主管单位', trigger: 'blur' }],


      },
      ruleslist: {

        // kucunhzsl: [{ required: true, message: '请输入数量', trigger: 'blur' }],

      },
      tabheight: 500,
      importShow: false,
      KufangList: [],
      cbdjList: [],
      Wzzylist: [],
      Zgdwlist: []
    }
  },
  created() { },
  mounted() {
    this.getKufangList();
    let height = document.getElementById("table-list").offsetHeight
    this.tabheight = height - 94
    this.ruleForm = Object.assign({}, this.datarow)
    this.$nextTick(() => {
      this.$refs.plxTable.reloadData(this.ruleForm.zhuanjidmxDtos)
    })
  },
  methods: {
    goBack() {
      this.$parent.addshow = false
    },

    DialogOpenWuzi(row) {
      this.$refs['wuzi-dialog'].showEdit(row)
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.zhuanjidmxEntities = this.$refs.plxTable.getTableData().fullData
          const mxLen = this.$refs.plxTable.getTableData().fullData
          if (mxLen.length == 0) {
            this.$message({
              message: '请添加明细',
              type: 'warning',
            });
            return;
          }
          this.$refs.plxTable.validate(true).then(async (data) => {
            if (data) {
              // 有错误
            }
            else {
              let ruleForm2 = Object.assign({}, this.ruleForm)
              delete ruleForm2.create_time
              delete ruleForm2.modify_time
              delete ruleForm2.create_user
              delete ruleForm2.modify_user
              delete ruleForm2.kaidanrq
              delete ruleForm2.jujueyy
              delete ruleForm2.zhuanjidh
              //  delete ruleForm2.id
              delete ruleForm2.zhuanjidmxDtos
              //   console.log(ruleForm2)
              ruleForm2.zhuanjidmxEntities.forEach(element => {
                delete element.create_time
                delete element.modify_time
                delete element.create_user
                delete element.modify_user
                delete element.id
                delete element._XID
              })
              const msg = await insertZhuanjid(ruleForm2)
              this.$message({
                message: msg.msg,
                type: 'warning',
              });
              if (this.gobacktitle == '新增') {
                this.resetForm();//重置表单
              }
              this.$parent.resetlist()

            }
          })
        } else {
          return false
        }
      })
    },

    resetForm(formName) {
      this.ruleForm = Object.assign({ mxList: [] }, [])
      this.$nextTick(() => {
        this.$refs.plxTable.reloadData([])
      })
      // this.$refs[formName].resetFields()
    },
    selectChange(e) { },
    // 添加插入行
    async insertEvent(row) {
      let record = this.insertData
      // 加入行, 表示加入行的row对象，row为空则插入第一行，为-1插入最后一行
      let { row: newRow } = await this.$refs.plxTable.insertRow(record)
      // 激活单元格编辑
      await this.$refs.plxTable.setActiveRow(newRow)
    },
    delwzDetail() {
      this.$refs.plxTable.remove(this.$refs.plxTable.getCheckboxRecords())
    },
    uploadSuccess() {
      this.importShow = false
    },
    oninput(num) {
      return num.replace(/\D/g, '')
    },
    async getKufangList() {
      //库房
      const kfData = await queryKufangList();
      this.KufangList = kfData.data

      //等级
      this.cbdjList = await this.$util.getZBDJlist()
      this.Zgdwlist = await this.$util.getZgdw()
      //物资分类
      this.Wzzylist = await this.$util.getWzzy()

    },
    fetchData(data) {
      this.insertData = data

      this.insertData.forEach(item => {
        item.wuziid = item.id
      });
      this.insertEvent();
    }

  },
}
</script>
<style lang="scss" scoped>
.add-container {
  position: fixed;
  left: 0;
  top: .75rem;
  width: 100%;
  height: 100%;
  z-index: 1000;
  background-color: #f6f8f9;
  padding: 0 .2rem 1rem .2rem;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  overflow: hidden;

  .add-top {
    padding: .10rem 0;

    ::v-deep .el-page-header__title {
      font-size: .16rem !important;
    }
  }

  .add-main {
    flex: 1;
    font-weight: bold;
    display: flex;
    flex-direction: column;
    overflow: auto;

    .add-main-top {
      padding: .2rem;
      background: #fff;
      border-radius: .05rem;

      .add-main-tital {
        font-size: .18rem;
        border-left: .10rem solid rgb(121, 189, 255);
        padding-left: .10rem;

        p {
          margin: 0;
        }
      }

      .search-gropu-item {
        margin-top: .20rem;
        width: 90%;
        display: flex;
        // label {
        //   white-space: nowrap;
        //   line-height: 32px;
        //   margin-right: 10px;
        // }
      }

      .el-select {
        width: 100%;
      }
    }

    .add-main-list {
      flex: 1;
      margin-top: 20px;
      background: #fff;
      padding: 20px;
      box-sizing: border-box;
      display: flex;
      flex-direction: column;

      .list-button-group {
        display: flex;
        flex-direction: row;
        padding-bottom: 20px;

        .button-item {
          padding: 0px 10px;
        }

        .button-item:first-child,
        &:last-child {
          padding: 0px 10px 0 0;
        }
      }

      .table-list {
        flex: 1;
      }
    }

    .el-button {
      font-size: .14rem !important;
    }

    ::v-deep {
      .elx-table--header-wrapper {
        background-color: rgb(233, 243, 255);
      }

      .elx-table {
        font-size: 0.14rem !important;
      }

      .elx-body--column {
        height: .48rem !important;
        line-height: .24rem;
      }

      .elx-header--column {
        background-image: none !important;
        height: .48rem !important;
      }

      .el-date-editor.el-input,
      .el-date-editor.el-input--inner {
        width: 100% !important;
      }

      .el-form-item--small.el-form-item {
        margin-bottom: 0px;
        margin-top: 18px;
      }
    }

    .submit-button {
      margin-top: 20px;
      display: flex;
      justify-content: center;
    }
  }
}
</style>